package org.compression.services.imp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.tomcat.jni.Mmap;
import org.compression.constant.Constant;
import org.compression.mapper.SearchMapper;
import org.compression.services.SearchService;
import org.compression.util.RedisUtil;
import org.compression.util.SetUtil;
import org.model.AttrValueRel;
import org.model.BaseModel;
import org.model.Condition;
import org.model.ConstantsEmnu;
import org.model.MagazineAttrs;
import org.model.MagazineValue;
import org.model.Newmagazine;
import org.model.Page;
import org.model.SearchHigh;
import org.model.SearchModel;
import org.model.SelectModel;
import org.model.TNewMagazine;
import org.model.business.BookAttrReturn;
import org.model.business.BookModel;
import org.model.business.BookTranlate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class SearchServiceImp implements SearchService{

	@Autowired
	SearchMapper mSearchMapper;


	@Autowired
	RedisUtil	mRedisUtil;


	@Override
	public Object getCategory() {
		return mSearchMapper.getCategory();
	}


	@Override
	public Object getMagazine() {
		return mSearchMapper.getMagazine();
	}


	@Override
	public Object getCategoryId(String id) {
		return mSearchMapper.getCategoryId(id);
	}

	@Override
	public Object getMaginzeId(String id) {
		List<Newmagazine> mMagaize=mSearchMapper.getMaginzeId(id);
		//	mMagaize.addAll(mSearchMapper.getMaginzedeflaut(id));
		return mMagaize;
	}




	@Override
	public Object getMaginzeName(String name) {
		return mSearchMapper.getMaginzeName(name);
	}

	@Override
	public void insertMaginze(TNewMagazine model) {
		mSearchMapper.insertMagazine(model);
	}


	@Override
	public List<MagazineAttrs> getMaginzeAttrId(String id) {
		return mSearchMapper.getMaginzeAttrId(id);
	}

	@Override
	public List<MagazineValue> getMaginzeValue(String id) {
		return mSearchMapper.getMaginzeValue(id);
	}


	@Override
	public void insertMagazinevalue(MagazineValue mMagazineValue) {
		mSearchMapper.insertMagazinevalue(mMagazineValue);
	}

	@Override
	public void insertMagazineattrs(MagazineAttrs mMagazineAttrs) {
		mSearchMapper.insertMagazineattrs(mMagazineAttrs);
	}


	@Override
	public void insertAttrValueRel(AttrValueRel mAttrValueRel) {
		mSearchMapper.insertAttrValueRel(mAttrValueRel);
	}


	@Override
	public List<Object> getHigSearch(List<String> id, List<String> values) {
		return mSearchMapper.getHigSearch(id, values);
	}


	SetUtil<String> mUtil=new SetUtil<>();









	//判断条件  与或非
	@Override
	public Object getHighSelect(Condition mCondition) throws Exception {
		SearchHigh mSearchHigh=new SearchHigh();
		try {
			if(Constant.getQueue().size()<1) {
				Constant.queue.put(mCondition);
			}else {
				return "0000";
			}

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



		while(!Constant.getQueue().isEmpty()) {

			mCondition=	Constant.queue.take();
			List<SelectModel> mHigSearch=mCondition.getmHigSearch();
			HashSet<String> newMagainzeId=new HashSet<String>();


			//查询条件  需要优化
			newMagainzeId.addAll(mSearchMapper.selectHigh(mHigSearch.get(0).getAttr(),mHigSearch.get(0).getValue()));
			mHigSearch.remove(0);

			List<String> endNewMagId=new ArrayList<String>();
			for(SelectModel mSelectModel:mHigSearch) {
				HashSet<String> compare;
				switch(mSelectModel.getCondition()) {
				//0 于 1或 2 非
				case 0:
					compare=mSearchMapper.selectHigh(
							mSelectModel.getAttr(),mSelectModel.getValue());
					//newmagainedId
					newMagainzeId=mUtil.jiaojiSet(compare,newMagainzeId);
					break;
				case 1:
					compare=mSearchMapper.selectHigh(mSelectModel.getAttr(),mSelectModel.getValue());
					newMagainzeId.addAll(compare);
					break;
					//
				case 2:
					//compare.clear();
					compare=mSearchMapper.selectNOT(mSelectModel.getAttr(),mSelectModel.getValue());
					newMagainzeId.addAll(compare);
					break;
				default:
					return "0000";

				};
			}


			//查询多少本
			//跳转的页数
			//Map mattval=(Map) getPage(mCondition.getmPage(),newMagainzeId,mSearchMapper,mSearchHigh,mRedisUtil);

			//查询 中共多少书
			mSearchHigh.setTotal(newMagainzeId.size());
			/*		mSearchHigh.setMattval(mattval);*/
			mSearchHigh.setArryBooks((List<Object>) (getSearch(newMagainzeId,mCondition.getmPage())));
			break;
		}

		return mSearchHigh;
		//	return mSearchHigh;
	}


	//存放在缓存中
	public static Object getPage(Page mPage,HashSet<String> newMagainzeId,SearchMapper mSearchMapper,Object mSearchHigh,RedisUtil mRedisUtil) {
		List<Object> testObject=new ArrayList<>();
		Map mattval=((SearchHigh) mSearchHigh).getMattval();
		//从第几条开始查
		int startpage=(mPage.getCurrentPage()-1)*mPage.getSize()+1;
		int endPage=mPage.getCurrentPage()*mPage.getSize();
		int total=newMagainzeId.size();
		//
		int flag=0;

		for(String id:newMagainzeId) {
			flag++;
			if(flag>=startpage && flag<=total && flag<=endPage) {
				Object m=mRedisUtil.get("org.compression.services.imp.name"+id);
				Object m1=mRedisUtil.get("org.compression.services.imp.value"+id);
				if(m==null&&m1==null) {
					//testObject.add(mSearchMapper.getMaginzeById(id));
					//testObject.add(mSearchMapper.getMaginzeId(id));
					mRedisUtil.set("org.compression.services.imp.name"+id,mSearchMapper.getMaginzeById(id));
					mRedisUtil.set("org.compression.services.imp.value"+id,mSearchMapper.getMaginzeId(id));
					mattval.put(mSearchMapper.getMaginzeById(id).getName(),mSearchMapper.getMaginzeId(id));
				}else {
					mattval.put(m,m1);
				}

			}
		}
		return mattval;
	}

	//分页查询
	public Object getSearch(HashSet<String> newMagainzeId,Page mPage) {
		List<Object> arryBooks=new ArrayList<>();
		int startpage=(mPage.getCurrentPage()-1)*mPage.getSize()+1;
		int endPage=mPage.getCurrentPage()*mPage.getSize();
		int total=newMagainzeId.size();
		//
		int flag=0;
		for(String id:newMagainzeId) {
			flag++;
			if(flag>=startpage && flag<=total && flag<=endPage) {
				Object mBook=mRedisUtil.get("newMagainzeId"+id);
				if(mBook==null) {
					mBook=	mSearchMapper.getMaginzeId(id);
					mRedisUtil.set("newMagainzeId"+id,mBook);
				}
				arryBooks.add(mBook);
			}
		}
		return arryBooks;
	}


	@Override
	public List<BaseModel> selectIdContional() {
		return mSearchMapper.selectIdContional();
	}


	@Override
	public Object getDesc() {
		return mSearchMapper.getDesc();
	}


	@Override
	public List<BaseModel> getAllBook() {
		return mSearchMapper.getAllBook();
	}

	//根据属性查询书id
	@Override
	public Object selectByIdListMagazineIds(BookAttrReturn mBookAttrReturn) {
		long total=mSearchMapper.countMagazineIds(mBookAttrReturn.getId(),mBookAttrReturn.getmPage().getCurrentPageNum(),mBookAttrReturn.getmPage().getPageSize());
		return  commSearch(mSearchMapper.selectByIdListMagazineIds(mBookAttrReturn.getId(),mBookAttrReturn.getmPage().getCurrentPageNum(),mBookAttrReturn.getmPage().getPageSize()),total);
	}


	@Override
	public	Object commSearch(List<String> ids,long size) {
		HashMap<String,Object>	mResults=(HashMap<String, Object>) commSearch(ids);
		mResults.put("total",size);
		return mResults;

	}


	@Override
	public Object commSearch(List<String> ids) {
		HashMap<String,Object>  mResults=new HashMap<>();
		List<BookModel> mBookModels=new ArrayList<>();
		List<BookTranlate> mBookTranlates=mSearchMapper.commSearch(ids);
		for(String id:ids) {
			BookTranlate temp=new BookTranlate();
			BookModel mBookModel=new BookModel();
			for(BookTranlate mBookTranlate:mBookTranlates) {
				if(id.equals(mBookTranlate.getNew_magazine_id()+"")) {
					if(mBookTranlate.getCode()!=null) {
						switch (mBookTranlate.getCode().trim()) {
						case ConstantsEmnu.bookcodeEISBN:
							mBookModel.setEISBN(mBookTranlate.getValue());
							break;
						case ConstantsEmnu.bookcodeauthor:
							mBookModel.setAuthor(mBookTranlate.getValue());
							break;
						case ConstantsEmnu.bookcodeimagePath:
							mBookModel.setImagePath(mBookTranlate.getValue());
							break;

						case ConstantsEmnu.bookcodeurl:
							mBookModel.setUrl(mBookTranlate.getValue());
							break;
							
						case ConstantsEmnu.bookcodepress:
							mBookModel.setPress(mBookTranlate.getValue());
							break;
						case ConstantsEmnu.bookcodebookName:
							mBookModel.setBookName(mBookTranlate.getValue());
							break;
							
						case ConstantsEmnu.bookcodelanguages:
							mBookModel.setLanguages(mBookTranlate.getValue());
							break;
						default :
							break;
						}
					}
					
				}
			}
			mBookModels.add(mBookModel);
		}
		mResults.put("books",mBookModels);
		return mResults;
	}









}
